package com.chzu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chzu.pojo.QuestionInfo;
import com.chzu.result.JsonResult;
import com.chzu.result.QueryInfo;
import com.chzu.service.QuestionInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName QuestionInfoController
 * @Description TODO
 * @Date 2022/02/21 19:17
 */
@RestController
@RequestMapping("/questionInfo")
@Api(tags = "题目信息的接口")
public class QuestionInfoController {

    @Resource
    private QuestionInfoService questionInfoService;

    @ApiOperation("查询所有题目信息")
    @PostMapping("/questionInfoPage")
    public JsonResult<Object> getAllQuestionInfo(@RequestBody QueryInfo queryInfo) {
        return questionInfoService.getAllQuestionInfo(queryInfo);
    }

    @ApiOperation("添加某个题目信息")
    @Transactional(rollbackFor= Exception.class)
    @PostMapping("/")
    public JsonResult<String> addQuestionInfo(@RequestBody QuestionInfo questionInfo) {
        questionInfo.setQuestionId(UUID.randomUUID().toString());
        boolean save = questionInfoService.save(questionInfo);
        if (!save) {
            return JsonResult.fail("添加试题目信息失败");
        }
        return JsonResult.ok("添加题目信息成功");
    }

    @ApiOperation("修改某个题目信息")
    @Transactional(rollbackFor= Exception.class)
    @PutMapping("/")
    public JsonResult<String> putQuestionInfo(@RequestBody QuestionInfo questionInfo) {
        QueryWrapper<QuestionInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("question_id",questionInfo.getQuestionId());
        boolean result = questionInfoService.update(questionInfo,wrapper);
        if (!result) {
            return JsonResult.fail("修改题目信息失败");
        }
        return JsonResult.ok("修改题目信息成功");
    }

    @ApiOperation("删除某个题目信息")
    @Transactional(rollbackFor= Exception.class)
    @DeleteMapping("/{questionId}")
    public JsonResult<String> deleteQuestionInfo(@PathVariable("questionId") String questionId) {
        QueryWrapper<QuestionInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("question_id",questionId);
        boolean result = questionInfoService.remove(wrapper);
        if (!result) {
            return JsonResult.fail("删除题目信息失败");
        }
        return JsonResult.ok("删除题目信息成功");
    }

    @ApiOperation("分页查询所有题目以及对应的选项")
    @PostMapping("/getQuestionInfoAndChose")
    public JsonResult<Object> getQuestionInfoAndChose(@RequestBody QueryInfo queryInfo) {
        return questionInfoService.getQuestionInfoAndChose(queryInfo);
    }
}
